//
//  OrderListViewController.m
//  Commerce
//
//  Created by gc on 15/3/5.
//  Copyright (c) 2015年 fei. All rights reserved.
//

#import "OrderListViewController.h"
#import "OrderCell.h"

@interface OrderListViewController (){
    NSMutableArray *_dataArray;
}


@end

@implementation OrderListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataArray = [[NSMutableArray alloc] initWithCapacity:10];
    [self refreshData];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    // Do any additional setup after loading the view from its nib.
}

- (void)getDataList{

    [self getOrderList];
}

#pragma mark -请求接口
- (void)getOrderList{//订单列表
    [SVProgressHUD showWithStatus:@"加载中" maskType:SVProgressHUDMaskTypeBlack];
    //传入的参数
    NSMutableDictionary *parameters = [GlobalData paramterDic];

    [parameters addEntriesFromDictionary:@{@"userid":[[GlobalData sharedGlobalData] userinfoDic][@"UserID"]}];
    NSLog(@"%@",parameters);
    NSString *url =nil;
    if (_menuID == 27) {//我的订单
        url = [NSString stringWithFormat:@"%@order/my",URL_ROOT];
    }else if (_menuID == 31){//车源订单
        url = [NSString stringWithFormat:@"%@order/mycar",URL_ROOT];
    }
    [[BaseNetwork alloc] initWithMethodUrl:url
                                    params:parameters
                               requestMode:DGHTTPMethodPOST
                               loadingShow:YES
                              didFinshLoad:^(id result) {
                                  id responseObject = result;
                                  [SVProgressHUD dismiss];
                                  NSLog(@"responseObject=%@",responseObject);
                                  if ([(NSArray *)responseObject count] < self.pageLimit) {
                                      self.canLoadMore = NO;
                                  }else{
                                      self.canLoadMore = YES;
                                  }
                                  if (self.page == 1) {
                                      [self.tableView setContentOffset:CGPointZero animated:NO];
                                      [_dataArray removeAllObjects];
                                      [_dataArray addObjectsFromArray:responseObject];
                                      [self addItemsOnTop];
                                  }else{
                                      [_dataArray addObjectsFromArray:responseObject];
                                      [self addItemsOnBottom];
                                  }

                              }];
}

#pragma mark - Table View Delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 66;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"Cell";
    
    OrderCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"OrderCell" owner:self options:nil];
        cell = (OrderCell *)[array objectAtIndex:0];
    }
    [cell configUIWithData:_dataArray[indexPath.row]];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
